package com.py.custom.business.demo.core.controller;

import com.py.custom.business.demo.core.mapstruct.BeanConvertUtils;
import com.py.custom.business.demo.core.service.StudentService;
import com.py.custom.business.demo.core.service.model.StudentDTO;
import com.py.custom.business.demo.protocol.groups.StudentGroup;
import com.py.custom.business.demo.protocol.request.StudentRequest;
import com.py.custom.business.demo.protocol.response.StudentResponse;
import com.py.custom.business.demo.protocol.vo.StudentVO;
import com.py.custom.common.web.response.BaseResponse;
import com.py.custom.common.web.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description：
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private BeanConvertUtils beanConvertUtils;

    @Autowired
    private StudentService studentService;

    @PostMapping("/getStudentList")
    @ResponseBody
    public BaseResponse<StudentResponse> getStudentList(@Validated(value = {StudentGroup.DefaultGroup.class})
                                                        @RequestBody StudentRequest studentRequest) {
        // 业务查询
        List<StudentDTO> studentDtoList = studentService.getStudentList(studentRequest);

        // 转换
        List<StudentVO> studentVOList = beanConvertUtils.studentDto2VoList(studentDtoList);

        // 返回结果
        StudentResponse studentResponse = new StudentResponse();
        studentResponse.setList(studentVOList);
        return ResponseUtils.success(studentResponse);
    }

    @PostMapping("/saveStudent")
    @ResponseBody
    public BaseResponse<Boolean> saveStudent(@Validated(value = {StudentGroup.SaveGroup.class})
                                             @RequestBody StudentRequest studentRequest) {
        boolean hasSave = studentService.saveStudent(studentRequest);
        return ResponseUtils.success(hasSave);
    }
}
